package com.fowo.api.sys.component;

import com.fowo.api.user.model.JwtUserInfo;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * 提供用户信息用于直接在 EL 表达式中使用
 * @author yl_ls
 */
@Component("userInfo")
public class UserInfoComponent {

    /**
     * 获取当前用户编号
     */
    public Long getUserId() {
        JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
        if (jwtUserInfo == null) {
            return null;
        }
        return jwtUserInfo.getUserId();
    }

    /**
     * 获取当前用户类型和编号组合的字符串
     */
    public String getUserTypeAndId() {
        JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
        if (jwtUserInfo == null) {
            return "#";
        }
        return String.format("%s#%d", jwtUserInfo.getType(), jwtUserInfo.getUserId());
    }
}
